<link rel="stylesheet" href="../static/css/style.css">
<link rel="stylesheet" href="../static/css/styleoneadd.css">
<link rel="stylesheet" href="../static/plugins/layui/css/layui.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../static/plugins/bootstrap-3.3.7/css/bootstrap.min.css">
<style>
	body{
		background-color: #f2f2f2 !important;
	}
</style>

<!-- 内容主体区域 -->
<div class="layui-fluid-pad" ng-app="localItermApp">
	<div class="layui-col-md12" ng-controller="localItermController" id="localItermController">
		<div class="layui-card">
			<div class="layui-card-header">平台单向库管理</div>
			<div class="layui-card-body">

				<div class="layui-row layui-pad15">
					<form class="layui-form" action="">
						<div class="layui-col-md12 layui-col-lh">
							<div class="layui-row layui-col-space15">
								<div class="layui-col-md2"><input type="text" name="title" placeholder="单项名称" class="form-control" ng-model="itermName" /></div>
								<div class="layui-col-md2"><input type="text" name="title" placeholder="单项代码" class="form-control" ng-model="itermCode" /></div>
								<div class="layui-col-md3">
									<div class="layui-row">
										<div class="layui-col-md12 layui-order-flexs">
											<input type="text" class="form-control layui-gd-widths" placeholder="时间" id="test1" ng-model="startTime">
											&nbsp;&nbsp;至&nbsp;&nbsp;
											<input type="text" class="form-control layui-gd-widths" placeholder="时间" id="test2" ng-model="endTime">
										</div>
									</div>
								</div>
								<div class="layui-col-md1">
									<a href="javascript:;" class="layui-btn  layui-t-h" ng-click="getPartItrms()">筛选</a>
								</div>
								<div class="layui-col-md4 layui-text-right">
									<a class="layui-btn layui-t-h creatdx" id="creat-dx">
										<i class="layui-icon">&#xe60a</i>
										创建单项
									</a>
								</div>
							</div>
						</div>
					</form>

					<!--表格-->
					<div class="layui-row">
						<div class="layui-col-md12">
							<table class="layui-data-zs" id="test3" border="1" style="border: 1px solid #e5e5e5;" ng-table="itermTable">
								<thead>
									<tr class="layui-t-h layui-hg-b" style=" background-color: #34C0E3;color: #ffffff;">
										<th>序号</th>
										<th>单项名称</th>
										<th>单项说明</th>
										<th>单项价格（元）</th>
										<th>单项代码</th>
										<th>创建时间</th>
										<th>操作</th>
									</tr>
								</thead>
								<tbody>
									<tr class="layui-hg-b" ng-repeat="iterm in localItermList">
										<td><span ng-bind="$index + 1"></span></td>
										<td><span ng-bind="iterm.name || '--'"></span></td>
										<td><span ng-bind="iterm.brief || '--'"></span></td>
										<td><span ng-bind="(iterm.price / 100 + '.' + iterm.price % 100) || '--'"></span></td>
										<td><span ng-bind="iterm.code || '--'"></span></td>
										<td><span ng-bind="(iterm.createdTime|date:'yyyy-MM-dd') || '--'"></span></td>
										<td>
											<a href="#" class="layui-btn-ty layui-zdy-bj " ng-click="modifyUniterm(iterm.id)">编辑</a>
											<a href="#" class="layui-btn-ty  layui-dels" ng-click="dltUniterm(iterm.id)">删除</a>
										</td>
									</tr>
								</tbody>
							</table>
							<div ng-show="!localItermList || localItermList.length == 0" style="margin: 20px 0; text-align: center;">无符合条件数据。</div>
						</div>
						<!--分页-->
						<div class="layui-col-md12 layui-page-top" ng-hide="!localItermList || localItermList.length == 0">
							<div id="demo7"></div>
						</div>
						<!--分页end-->
					</div>
					<!--表格end-->

				</div>
			</div>
		</div>
	</div>
</div>

<script src="../static/js/jquery.min.js"></script>
<script src="../static/plugins/layui/layui.js"></script>
<script src="../static/plugins/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../static/js/angular.js"></script>
<script src="../static/js/app/consts/consts.js"></script>
<script>

    var pageConut = 0;
    var pageSize = 10;
    var pageNum = 1;
    var currentLocalUniterms = [];

    var dltLocalUnitermURL = baseUrl +"/exam/dltLocalUniterm";
    var getPartlLocalUnitermURL = baseUrl + "/exam/getPartlLocalUniterm";
    var getAllLocalUnitermURL = baseUrl + "/exam/getAllLocalUniterm";

	var app = angular.module("localItermApp", ['ng']);
	app.controller('localItermController', function($scope, $http) {

		$(function(){
            updateTable();
		});

		//删除按钮
		$scope.dltUniterm = function (localUnitermId) {
            layer.open({
                area: '300px;',
                title: '确认',
                content: '确认删除？',
                btn: ['确认', '取消'],
                closeBtn: false,
                resize: false,
                yes: function (index, layero) {
                    var data = {
                        token: "123456",
                        localUnitermId: localUnitermId
                    }
                    $http({
                        method: "POST",
                        url: dltLocalUnitermURL,
                        data: data,
                        headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
                        transformRequest: function (obj) {
                            var str = [];
                            for (var p in obj) {
                                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                            }
                            return str.join("&");
                        }
                    }).then(function successCallback(result) {
                        console.log(result.data.message);
                        $scope.getPartItrms(pageNum);
                        layer.msg("删除成功");
                    });
                }
        	});
		}

		//筛选按钮
		$scope.getPartItrms = function(goPageNumber) {
            if(!goPageNumber) {
                pageNum = 1;
            } else {
                pageNum = goPageNumber;
            }
			var data = {
				token:"22222",
				localUnitermName:$scope.itermName?($scope.itermName):"",
				code:$scope.itermCode?($scope.itermCode):"",
				startTime:$('#test1').val()?($('#test1').val()):"",
				endTime:$('#test2').val()?($('#test2').val()):"",
                pageNumber: pageNum,
                pageSize: pageSize,
                sort: "",
                desc: ""
			};
			$http({
				method:"POST",
				url:getPartlLocalUnitermURL,
				data:data,
				headers:{'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
		        transformRequest: function(obj) {
		            var str = [];
		            for(var p in obj){
		                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
		            }
		            return str.join("&");
		        }
			}).then(
			    function successCallback(result) {
                    if(result.data) {
                        var resultInfo = result.data.data;
                        updateUnitermPagination(resultInfo);
                        if(resultInfo) {
                            $scope.localItermList = resultInfo.data;
                        }
                        if(result.data.msg) {
                            layer.msg(result.data.msg)
                        }
                    }
				},
				function errorCalback(result) {
					console.log("error");
                }
			);
		}

        function updateTable() {
            $http({
                method: "GET",
                url: getAllLocalUnitermURL,
                params: {
                    token: "12312312312312",
                    pageNumber: pageNum,
                    pageSize: pageSize,
                    sort: "",
                    desc: ""
                }
            }).then(function successCallback(result) {
                if(result.data) {
                    var resultInfo = result.data.data;
                    updateUnitermPagination(resultInfo);
                    if(resultInfo) {
                        $scope.localItermList = resultInfo.data;
                        console.log($scope.localItermList);
                    }
//                    if(result.data.msg) {
//                        layer.msg(result.data.msg)
//                    }
                }
            });
        }
	});

    //更新分页数据
    function updateUnitermPagination(resultInfo) {
        pageConut = resultInfo.total || 0;
        pageSize = resultInfo.pageSize || 10;
        pageNum = resultInfo.pageNumber || 1;
        currentLocalUniterms = resultInfo;
        refreshPagination();
    }

    // 分页刷新
    function refreshPagination() {
        layui.use(['laypage', 'layer'], function() {
            var laypage = layui.laypage;
            //分页
            laypage.render({
                elem: 'demo7'
                ,count: pageConut
                ,curr: pageNum
                ,theme: '#34C0E3'
                ,limit: pageSize
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                ,jump: function(obj, first) {
                    if (!first) {
                        pageNum = obj.curr;
                        pageSize = obj.limit;
                        var scope = getAngularScope();
                        scope.getPartItrms(pageNum);
                        console.log(pageSize + ":" + pageNum);
                    }
                }
            });
        });
    }

    // 获取angular scope
    function getAngularScope() {
        return angular.element(localItermController).scope();
    }

    //Demo
    layui.use(['form', 'laypage', 'laydate', 'table', 'layer'], function() {
        var form = layui.form;
        var laydate = layui.laydate;
        var laypage = layui.laypage
        //监听提交
        form.on('submit(formDemo)', function(data) {
            layer.msg(JSON.stringify(data.field));
            return false;
        });
        // 日期
        laydate.render({
            elem: '#test1' //指定元素
            ,theme: '#34C0E3'
        });
        laydate.render({
            elem: '#test2' //指定元素
            ,theme: '#34C0E3'
        });
        var scope = getAngularScope();
        //编辑按钮
        scope.modifyUniterm = function (localUnitermId) {
            console.log(localUnitermId);
            var strU = 'createUniterm.html?unitermId='+localUnitermId;
            layer.open({
                type: 2,
                title: '编辑单项',
                area: ['480px', '380'],
                shadeClose: true, //开启遮罩关闭
                content: strU
            });
        }
        //创建单项
        $(function() {
            $('.creatdx').on('click', function() {
                var strU = 'createUniterm.html';
                layer.open({
                    type: 2,
                    title: '创建单项',
                    area: ['480px', '380'],
                    shadeClose: true, //开启遮罩关闭
                    content: strU
                });
            });
        });
    });


</script>